#!/usr/bin/perl
use Getopt::Std;
getopt("s:m:n", \%args);
$sample = $args{s};
chomp $sample;
$newsample = $args{n};
chomp $newsample;
$marker = $args{m};
chomp $marker;
if($sample eq "")
{
        die "entered samplefile name  is empty\n";
}
if($marker eq "")
{
        die "entered marker file name is empty\n";
}
if($newsample eq "")
{
        die "entered newmarker file name is empty\n";
}
open(SAM,$sample) or die "no $sample file found\n";
open(MBUFF,$marker) or die " no $marker file found\n";
open(WRBUFF,">$newsample");
$line = <SAM>;
print WRBUFF $line;
$line = <SAM>;
print WRBUFF $line;
#STROING IN THE HASH THE RSIDS
while($line = <SAM>)
{
        chomp $line;
        @array = split(/\s/,$line);
        shift(@array);
        $rsid_sam = shift(@array);
	$sam_hash{$rsid_sam} = 1;
}
#creating order and storing alleles
$order = 0;	
while($line = <MBUFF>)
{
	chomp $line;
	$line =~ s/\n//g;
	$line =~ s/\r//g;
	@array_sam = split(" ",$line);
	$rsid_sam = shift(@array_sam);
	shift(@array_sam);
	if(exists($sam_hash{$rsid_sam}))
	{
		$hash_sam{$rsid_sam} =$order;
		$order++;
		if($array_sam[0] ne "")
		{
			$hash_sam0{$rsid_sam} = $array_sam[0];
		}
		if($array_sam[1] ne "")
		{
			$hash_sam1{$rsid_sam} = $array_sam[1];
		}
	}
}
open(SAM,$sample) or die "no $sample file found\n";
$line = <SAM>;
$line = <SAM>;
@order_1 =();
while($line = <SAM>)
{
	chomp $line;
	@array = split(/\s/,$line);
	shift(@array);
        $rsid_sam = shift(@array);
	chomp $rsid_sam;
	if(exists($hash_sam{$rsid_sam}))
	{
	  	if(exists($hash_sam1{$rsid_sam}) || exists($hash_sam0{$rsid_sam}))
		{	if($line =~ m/$hash_sam0{$rsid_sam}/ || $line =~ m/$hash_sam1{$rsid_sam}/)
			{
			}
			else
			{
				$line =~ tr/ATGC/TACG/;
			}
	 	}
		
	}
	#print WRBUFF $line."\n";
	$order_1[$hash_sam{$rsid_sam}] = $line;
}
for($i=0;$i<@order_1;$i++)
{
	print WRBUFF $order_1[$i]."\n";
}
